﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using OgilvyOne.Model;
using OgilvyOne.BLL;

namespace OgilvyOne.BLL
{
    /// <summary>
    /// 标签代理类
    /// </summary>
    class LabelProxy
    {
        /// <summary>
        /// 得到labelstr对应标签
        /// </summary>
        /// <param name="model"></param>
        /// <param name="labelstr"></param>
        /// <returns></returns>
        public static ICmsLabel GetCmsLabel(CommonModel model, string labelstr)
        {
            try
            {
                XmlDocument doc;
                doc = new XmlDocument();
                XmlDeclaration decl = doc.CreateXmlDeclaration("1.0", "UTF-8", null);
                doc.AppendChild(decl);
                doc.LoadXml("<cms:label xmlns:cms=\"http://www.cofco.com.cn/cms/CMSSchema\">" + labelstr + "</cms:label>");
                XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);
                //直接方法内容string 替换
                nsmgr.AddNamespace("cms", "http://www.cofco.com.cn/cms/CMSSchema");

                if (labelstr.IndexOf("<cms:content")>-1)
                {//<csm:content 标签
                    XmlNode node = doc.SelectSingleNode("//cms:content", nsmgr);
                    ICmsLabel cl = new ContentLabel(model);
                    cl.LabelStr = labelstr;
                    if (node.Attributes["value"] != null)
                    {
                        cl.MethodName = node.Attributes["value"].InnerText;
                    }
                    return cl;
                }
                else if(labelstr.IndexOf("<cms:labelcontent")>-1)
                {//<cms:labelcontent 标签
                    XmlNode node = doc.SelectSingleNode("//cms:labelcontent", nsmgr);
                    LabelContentLabel cl = new LabelContentLabel(model);
                    cl.LabelStr = labelstr;
                    if (node.Attributes["value"] != null)
                    {
                        cl.LabelName = node.Attributes["value"].InnerText;
                    }
                    
                    return cl;
                }
                else if (labelstr.IndexOf("<cms:list") > -1)
                {
                    XmlNode node = doc.SelectSingleNode("//cms:list", nsmgr);
                    ListLabel cl = new ListLabel(model);
                    cl.LabelStr = labelstr;
                    if (node.Attributes["value"] != null)
                    {
                        cl.MethodName = node.Attributes["value"].InnerText;
                        cl.LabelNodeStr = node.Value;
                        if (node.Attributes["rows"] != null)
                        {
                            cl.Rows = int.Parse(node.Attributes["rows"].InnerText);
                        }
                    }
                    
                    return cl;
                }
                else if (labelstr.IndexOf("<cms:labellist") > -1)
                {
                    XmlNode node = doc.SelectSingleNode("//cms:labellist", nsmgr);
                    LabelListLabel cl = new LabelListLabel(model);
                    cl.LabelStr = labelstr;
                    if (node.Attributes["value"] != null)
                    {
                        cl.LabelName = node.Attributes["labelname"].InnerText;
                        if (node.Attributes["rows"] != null)
                        {
                            cl.Rows = int.Parse(node.Attributes["rows"].InnerText);
                        }
                        if (node.Attributes["method"] != null)
                        {
                            cl.MethodName = node.Attributes["method"].InnerText;
                        }
                    }
                    
                    return cl;
                }
                else if (labelstr.IndexOf("<cms:sqllist") > -1)
                {
                    XmlNode node = doc.SelectSingleNode("//cms:sqllist", nsmgr);
                    SqlListLabel cl = new SqlListLabel(model);
                    cl.LabelStr = labelstr;
                    if (node.Attributes["value"] != null)
                    {
                        //cl.MethodName = node.Attributes["value"].InnerText;
                        cl.LabelNodeStr = node.InnerText;
                        cl.Sqlstr = node.Attributes["value"].InnerText;
                        
                    }

                    return cl;
                }
                else if (labelstr.IndexOf("<cms:labelsqllist") > -1)
                {
                    XmlNode node = doc.SelectSingleNode("//cms:labelsqllist", nsmgr);
                    LabelSqlListLabel cl = new LabelSqlListLabel(model);
                    cl.LabelStr = labelstr;
                    if (node.Attributes["value"] != null)
                    {
                        cl.LabelName = node.Attributes["value"].InnerText;
                    }
                    return cl;
                }
                else if (labelstr.IndexOf("<cms:page") > -1)
                {
                    XmlNode node = doc.SelectSingleNode("//cms:page", nsmgr);
                    PageLabel cl = new PageLabel(model);
                    cl.LabelStr = labelstr;
                    if (node.Attributes["value"] != null)
                    {
                        cl.MethodName = node.Attributes["value"].InnerText;
                        if (node.Attributes["pagesize"] != null)
                        {
                            cl.Pagesize = int.Parse(node.Attributes["pagesize"].InnerText);
                        }

                        XmlNode columnnode = node.SelectSingleNode("//column");
                        cl.LabelNodeStr = columnnode.Value;
                        XmlNode pagenode = node.SelectSingleNode("//page");
                        cl.PageStr = pagenode.Value;
                    }
                    return cl;
                }
                else if (labelstr.IndexOf("<cms:labelpage") > -1)
                {
                    XmlNode node = doc.SelectSingleNode("//cms:labelpage", nsmgr);
                    LabelPageLabel cl = new LabelPageLabel(model);
                    cl.LabelStr = labelstr;
                    if (node.Attributes["value"] != null)
                    {
                        cl.LabelName = node.Attributes["value"].InnerText;
                        if (node.Attributes["method"] != null)
                        {
                            cl.MethodName = node.Attributes["method"].InnerText;
                        }
                        if (node.Attributes["pagesize"] != null)
                        {
                            cl.PageSize = int.Parse(node.Attributes["pagesize"].InnerText);
                        }
                    }
                    return cl;
                }
                else if (labelstr.IndexOf("<cms:menu") > -1)
                {
                    XmlNode node = doc.SelectSingleNode("//cms:menu", nsmgr);
                    MenuLabel cl = new MenuLabel(model);
                    cl.LabelStr = labelstr;
                    if (node.Attributes["value"] != null)
                    {
                        cl.MethodName = node.Attributes["value"].InnerText;
                        cl.Cid = int.Parse(node.Attributes["id"].InnerText);
                    }
                    return cl;
                }
                else if (labelstr.IndexOf("<cms:labelmenu") > -1)
                {
                    XmlNode node = doc.SelectSingleNode("//cms:labelmenu", nsmgr);
                    LabelMenuLabel cl = new LabelMenuLabel(model);
                    cl.LabelStr = labelstr;
                    if (node.Attributes["value"] != null)
                    {
                        cl.LabelName = node.Attributes["value"].InnerText;
                    }
                    return cl;
                }
                else if (labelstr.IndexOf("<cms:label") > -1)
                {
                    XmlNode node = doc.FirstChild;
                    if (node.Attributes["value"] != null)
                    {
                        return GetCmsLabel(model, labelstr, node.Attributes["value"].InnerText);
                    }
                }
            }
            catch
            {

            }
            ICmsLabel errorlabel = new ErrorLabel(model);
            errorlabel.LabelStr = labelstr;
            return errorlabel;
        }


        public static ICmsLabel GetCmsLabel(CommonModel model, string labelname, string labelstr)
        {
            string ltype = ((TemplateLabelModel)new TemplateLabelBll().GetLabelMap()[labelname]).LabelType;
            if (ltype != null)
            {
                LabelCommonLabel cl = null;
                ltype = ltype.Trim();
                switch (ltype)
                {
                    case "content":
                        cl = new LabelContentLabel(model);
                        break;
                    case "method":
                        cl = new LabelMethodContentLabel(model);
                        break;
                    case "list":
                        cl = new LabelListLabel(model);
                        break;
                    case "page":
                        cl = new LabelPageLabel(model);
                        break;
                    case "menu":
                        cl = new LabelMenuLabel(model);
                        break;
                    case "sqllist":
                        cl = new LabelSqlListLabel(model);
                        break;
                }
                if (cl != null)
                {
                    cl.LabelName = labelname;
                    cl.LabelStr = labelstr;
                    return cl;
                }
            }
            ICmsLabel errorlabel = new ErrorLabel(model);
            errorlabel.LabelStr = labelstr;
            return errorlabel;
        }

        public static ICmsLabel GetCmsLabelLabel(CommonModel model, string labelname)
        {
            return GetCmsLabel(model, labelname, "<cms:label value=\""+labelname+"\"></cms:label>");
        }
    }
}
